Logging
As apps are executed very frequently (once a second or so), unlimited logging can lead to huge amounts of data. corva-sdk provides a Logger object, which is a safe way for app logging.
The Logger is a logging.Logger instance and should be used like every other Python logger.
The Logger has following features:
- Log messages are injected with contextual information, which makes it easy to filter through logs while debugging issues.
- Log messages have limited length. Too long messages are truncated to not exceed the limit. Max message size can be controlled by LOG_THRESHOLD_MESSAGE_SIZE env variable. Default value is 1000 symbols or bytes.
- Number of log messages is limited. After reaching the limit logging gets disabled. Number of log messages can be controlled by LOG_THRESHOLD_MESSAGE_COUNT env variable. Default value is 15 messages.
- Logging level can be set using LOG_LEVEL env variable. Default value is INFO, see Python log levels for other available options.
from corva import Api, Logger, TaskEvent, task
@task
def task_app(event: TaskEvent, api: Api):
Logger.debug('Debug message!')
Logger.info('Info message!')
Logger.warning('Warning message!')
Logger.error('Error message!')
try:
0 / 0
except ZeroDivisionError:
Logger.exception('Exception message!')
- Import Logger object.
- Use Logger as every other Python logger.
1. Customizations
You might want to send logs to other places (e.g., to error reporting systems like Sentry or Rollbar). This can be achieved by providing an instance of logging handler as an argument to app decorator. Custom handler will be used alongside corva-sdk's default one.
import logging
from corva import Api, Logger, TaskEvent, task
stream_handler = logging.StreamHandler()
@task(handler=stream_handler)
def task_app(event: TaskEvent, api: Api):
Logger.info('Info message!')
- Import the module which contains the handler that we want to use.
- Initialize the handler.
- Pass the handler as a keyword argument to the app decorator.
- Logs will be sent to both stream_handler and corva-sdk's default one.
1.1. Sentry
Sentry for Python documentation.
pip install sentry-sdk
import sentry_sdk
from corva import Api, TaskEvent, task
sentry_sdk.init("YOUR_SENTRY_DSN")
@task
def app(event: TaskEvent, api: Api) -> None:
1 / 0
- Import Sentry SDK.
- Initialize the library.
- All errors will be reported to Sentry now.
1.2. Rollbar
Rollbar for Python documentation.
pip install rollbar
Install the library.
import rollbar.logger
from corva import Api, TaskEvent, task
rollbar_handler = rollbar.logger.RollbarHandler('YOUR_ROLLBAR_ACCESS_TOKEN')
@task(handler=rollbar_handler)
def app(event: TaskEvent, api: Api) -> None:
1 / 0
- Import Rollbar SDK.
- Initialize Rollbar handler.
- Pass the handler as a keyword argument to the app decorator.
- All errors will be reported to Rollbar now.
1.3. Raygun
Raygun for Python documentation.
pip install raygun4py
Install the library.
import raygun4py.raygunprovider
from corva import Api, TaskEvent, task
raygun_handler = raygun4py.raygunprovider.RaygunHandler('YOUR_RAYGUN_API_KEY')
@task(handler=raygun_handler)
def app(event: TaskEvent, api: Api) -> None:
1 / 0
- Import Raygun SDK.
- Initialize Raygun handler.
- Pass the handler as a keyword argument to the app decorator.
- All errors will be reported to Raygun now.
1.4. Other libraries
You can use any other error logging libraries. Just initialize and pass corresponding logging handler as a keyword argument to the app decorator. Use code samples above as the examples.